var http = require('http')
var fs = require('fs')
http.createServer((req, res) => {
  if (req.url === '/') {
    fs.readFile('./index.html', {}, (err, data) => {
      res.end(data)
    })
  }
  // 代理代码
  else if (req.url === '/login') {
    // 作为客户端代理，接受数据的过程
    var proxyRequest = http.request(`http://localhost:8088${req.url}`, responce => {
      console.log(res)
      responce.on('data', chunk => {
        res.write(chunk)
      })
      responce.on('end', () => {
        res.end()
      })
    })

    // 作为客户端代理，转发请求的过程
    req.on('data', data => {
      proxyRequest.write(data)
    })
    req.on('end', () => {
      proxyRequest.end()
    })
  }
}).listen(8080)